﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Slb.Ocean.Core;
using Slb.Ocean.Petrel.IO.Attributes;
using Slb.Ocean.Petrel.DomainObject;

namespace Public.Ocean.Petrel.Extensions.Attributes
{
    class MockCustomAttributeService : ICustomAttributeService
    {
        Dictionary<Droid, Dictionary<CustomAttributeKey, string>> data;

        public MockCustomAttributeService()
        {
            data = new Dictionary<Droid, Dictionary<CustomAttributeKey, string>>();
        }

        private Dictionary<CustomAttributeKey, string> Data(Droid droid)
        {
            Dictionary<CustomAttributeKey, string> result;
            if (!data.TryGetValue(droid, out result))
            {
                result = new Dictionary<CustomAttributeKey, string>();
                data.Add(droid, result);
            }
            return result;
        }

        private void Set(Droid droid, CustomAttributeKey key, string value)
        {
            Data(droid)[key] = value;
        }

        private string Get(Droid droid, CustomAttributeKey key)
        {
            string result;
            if (Data(droid).TryGetValue(key, out result))
            {
                return result;
            }
            else
            {
                return null;
            }
        }

        public void AddAttribute(Droid droid, IDataSourceManager mgr, CustomAttributeKey key, string value)
        {
            Set(droid, key, value);
        }

        public void AddAttributes(Droid droid, IDataSourceManager mgr, IDictionary<CustomAttributeKey, string> attributes)
        {
            foreach (var entry in attributes)
            {
                Set(droid, entry.Key, entry.Value);
            }
        }

        public bool CanHaveAttributes(Droid droid, IDataSourceManager mgr)
        {
            return true;
        }

        public void ClearAttributes(Project project)
        {
            data.Clear();
        }

        public string GetAttribute(Droid droid, IDataSourceManager mgr, CustomAttributeKey key)
        {
            return Get(droid, key);
        }

        public IDictionary<CustomAttributeKey, string> GetAttributes(Droid droid, IDataSourceManager mgr)
        {
            return Data(droid);
        }

        public ICollection<CustomAttributeKey> GetKeys(Droid droid, IDataSourceManager mgr)
        {
            return Data(droid).Keys;
        }

        public bool HasAttribute(Droid droid, IDataSourceManager mgr, CustomAttributeKey key)
        {
            return Get(droid, key) != null;
        }

        public void RemoveAttribute(Droid droid, IDataSourceManager mgr, CustomAttributeKey key)
        {
            Set(droid, key, null);
        }

        public void SetAllAttributes(Droid droid, IDataSourceManager mgr, IDictionary<CustomAttributeKey, string> attributes)
        {
            foreach (var entry in attributes)
            {
                Set(droid, entry.Key, entry.Value);
            }
        }

        public void SetAttribute(Droid droid, IDataSourceManager mgr, CustomAttributeKey key, string value)
        {
            Set(droid, key, value);
        }
    }
}